package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GrandChildGroupConfigDaoImpl extends TempDao {

    public static String selectSql = "select * from `t_s_grand_child_group_config` where channelId=? and state=1";

    public Map<Integer, List<Long>> getGrandChildGroupConfigMap(int channelId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<Long>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(selectSql);
            int i = 1;
            call.setInt(i++, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                int groupId = rs.getInt("groupId");
                String serverListStr = rs.getString("serverList");
                List<Long> serverList = StringUtils.stringToLongList(serverListStr, ",");
                map.put(groupId, serverList);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
